package hero.mps.sort;

import java.util.Arrays;

public class BubbleSort {
    public static void sort(int arr[]) {
        int temp = 0;
        for (int i = 0 ; i < arr.length; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j] > arr[j+1]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

    public static void sort1(int arr[]) {
        int temp = 0;
        for (int i = 0 ; i < arr.length; i++) {
            boolean isSorted = true;
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j] > arr[j+1]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    isSorted = false;
                }
            }
            if (isSorted) {
                break;
            }
        }
    }

    public static void sort2(int arr[]) {
        int temp = 0;
        int lastChangeIndex = 0;
        int sortBorder = arr.length - 1;
        for (int i = 0 ; i < arr.length; i++) {
            // 标记当前轮次是否排序完成
            boolean isSorted = true;
            for (int j = 0; j < sortBorder; j++) {
                // 如果当前元素大于下一个元素，则交换它们的位置
                if (arr[j] > arr[j+1]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    // 标记当前轮次排序未完成
                    isSorted = false;
                    // 更新最后一次交换的位置
                    lastChangeIndex = j;
                }
            }
            // 更新下一轮排序的边界
            sortBorder = lastChangeIndex;
            // 如果当前轮次已经排序完成，则跳出循环
            if (isSorted) {
                break;
            }
        }
    }

    public static void cookTailSort(int arr[]) {
        int temp = 0;
        for (int i = 0; i < arr.length / 2; i++) {
            boolean isSorted = true;
            for (int j = i; j < arr.length-1; j++) {
                if (arr[j] > arr[j+1]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    isSorted = false;
                }
            }
            if (isSorted) break;
            isSorted = true;
            for (int j = arr.length-i-1; j > i; j--) {
                if (arr[j] < arr[j-1]) {
                    temp =arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = temp;
                    isSorted = false;
                }
            }
            if (isSorted) break;
        }
    }

    public static void cookTailSort1(int arr[]) {
        int temp = 0;
        int lastRightExchangeIndex = 0;
        int lastLeftExchangeIndex = arr.length - 1;
        int rightSortBorder = arr.length - 1;
        int leftSortBorder = 0;
        for (int i = 0; i < arr.length / 2; i++) {
            boolean isSorted = true;
            for (int j = leftSortBorder; j < rightSortBorder; j++) {
                if (arr[j] > arr[j+1]) {
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    isSorted = false;
                    lastRightExchangeIndex = j;
                }
            }
            rightSortBorder = lastRightExchangeIndex;
            if (isSorted) break;
            isSorted = true;
            for (int j = rightSortBorder; j > leftSortBorder; j--) {
                if (arr[j] < arr[j-1]) {
                    temp =arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = temp;
                    isSorted = false;
                    lastLeftExchangeIndex = j;
                }
            }
            leftSortBorder = lastLeftExchangeIndex;
            if (isSorted) break;
        }
    }

    public static void main(String[] args) {
        int arr[] = {64, 34, 25, 11, 12, 22, 90};
        cookTailSort1(arr);
        System.out.println(Arrays.toString(arr));
    }
}
